#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int N = 1010;
int n, m;
int g[N][N];
bool vis[N][N];
int dx[] = { -1,1,0,0 };
int dy[] = { 0,0,-1,1 };

int bfs(int x, int y) {
	queue<pair<int, int>> q;
	vis[x][y] = 1;
	q.push({ x,y });
	int res = 0;
	while (!q.empty()) {
		int a = q.front().first;
		int b = q.front().second;
		q.pop();
		res += g[a][b];
		for (int i = 0; i < 4; i++) {
			int nx = a + dx[i], ny = b + dy[i];
			if (nx<1 || nx>n || ny<1 || ny>m) continue;
			if (vis[nx][ny] || g[nx][ny] == 0) continue;
			vis[nx][ny] = 1;
			q.push({ nx,ny });
		}
	}
	return res;
}

void solve() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> g[i][j];
			vis[i][j] = 0;
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (!vis[i][j] && g[i][j] > 0) {
				ans = max(ans, bfs(i, j));
			}
		}
	}
	cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t; cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}